package com.canis.stockmanager.web;

import com.canis.stockmanager.comm.aop.LoggerManage;
import com.canis.stockmanager.domai.Operator;
import com.canis.stockmanager.domai.Resource;
import com.canis.stockmanager.domai.result.Combox;
import com.canis.stockmanager.domai.result.ExceptionMsg;
import com.canis.stockmanager.domai.result.ResponseData;
import com.canis.stockmanager.repository.OperatorRepository;
import com.canis.stockmanager.utils.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/operator")
public class OperatorController extends BaseController {
    @Autowired
    private OperatorRepository  operatorRepository;
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData query(Model model, @RequestParam(value = "start", defaultValue = "0") Integer start, @RequestParam(value = "sort", defaultValue = "") String sortStr,
                              @RequestParam(value = "limit", defaultValue = "10") Integer limit, Operator operator, HttpServletResponse response) {
        Pageable pageable = util.getPageable(sortStr,start,limit);
        Page<Operator> pages=operatorRepository.findByNameContainingAndPhoneContainingAndAddressContaining(operator.getName(),operator.getPhone(),operator.getAddress(),pageable);

        List<Operator> lists=pages.getContent();
        return new ResponseData(ExceptionMsg.SUCCESS, "",pages.getTotalElements(),lists);
    }
    @RequestMapping(value="/",method= RequestMethod.GET)
    public String operator(Model model) {

        return "operator";
    }
    @RequestMapping(value = "/queryCB", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData queryCB(Model model, @RequestParam(value = "query", defaultValue = "") String query, HttpServletResponse response) {
        Pageable pageable = util.getPageable("id",0,50);
        List<Combox> cbs=operatorRepository.findByNameContaining(query);

        return new ResponseData(ExceptionMsg.SUCCESS, "",0,cbs);
    }
}
